 /**
 * 逻辑、功能相关描述: <br>
 *
 * @author 付湘辉  <br>
 * 编写日期：2008-7-28  <br>
 */
package com.ce.pms.customer.service.impl;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.common.model.SysInfo;
import com.ce.pms.customer.dao.IProRightDao;
import com.ce.pms.customer.model.ProRightDO;
import com.ce.pms.customer.service.IProRightService;
import com.ce.pms.customer.vo.CustomerHouseVO;
import com.ce.pms.customer.vo.ProRightVO;
import com.ce.pms.house.dao.IFlatDao;
import com.ce.pms.house.model.HouseDomain;
import com.ce.util.BeanUtil;
import com.ce.util.BlankUtil;

/**
 * 	名称: com.ce.pms.customer.service.impl.ProRightServiceImpl <br>
 *
 *
 * @author  付湘辉  <br>
 * Created on： 2008-7-28 <br>
 * @author <br>
 * update on： <br>
 * why: <br>
 */
public class ProRightServiceImpl extends PmsBaseServiceImpl implements
		IProRightService {
	/**
	 * 住宅房屋Dao
	 */
	private IFlatDao flatDao;

	/**
	 * 产权Dao
	 */
	private IProRightDao proRightDao;

	/**
	 * 功能描述: 新增产权信息 <br>
	 * @param proRightVo
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-28  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public ProRightVO addProRight(ProRightVO proRightVo){
		super.checkParameterIsNull(proRightVo, "common:parameters_null_exception, ");
		proRightVo.checkRequiredField();
		proRightVo.setValidity(SysInfo.VALIDITY);
		ProRightDO proRightDo = this.assembleV2D(proRightVo);
		this.proRightDao.saveObject(proRightDo);
		proRightVo.setProRightId(proRightDo.getProRightId());
		return proRightVo;
	}

	/**
	 * 功能描述:  <br>
	 * @param proRightVo
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-28  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public ProRightVO assembleV2V(CustomerHouseVO chvo){
		super.checkParameterIsNull(chvo, "common:parameters_null_exception, ");
		ProRightVO proRightVo = new ProRightVO();
		proRightVo.setHouseId(chvo.getHouseId());
		proRightVo.setCreator(chvo.getCreator());
		proRightVo.setContactNO(chvo.getHouseContractNumber());
		proRightVo.setProRightNO(chvo.getHousePropertyNo());
		return proRightVo;
	}

	/**
	 * 功能描述: 修改产权信息 <br>
	 * @param proRightVo
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-28  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public ProRightVO editProRight(ProRightVO proRightVo){
		super.checkParameterIsNull("common:parameters_null_exception, ",proRightVo,proRightVo.getProRightId());
		proRightVo.checkRequiredField();
		ProRightDO proRightDo = this.assembleV2D(proRightVo);
		this.proRightDao.updateObject(proRightDo);
		proRightVo.setModifyDate(proRightDo.getSysInfo()==null?null:proRightDo.getSysInfo().getModifyDate());
		return proRightVo;
	}

	/**
	 * 功能描述: 根据房间的id查找有效产权vo <br>
	 * @param houseId
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-28  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public ProRightVO getEffectiveProRightByHouseId(Long houseId){
		super.checkParameterIsNull(houseId, "common:parameters_null_exception,ID");
		ProRightDO proRightDo = this.proRightDao.getEffectiveProRightByHouseId(houseId);
		return this.assembleD2V(proRightDo);
	}

	/**
	 * 功能描述: 删除一个产权 <br>
	 * @param houseId 产权房id
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-10-23  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public Boolean deleteEffectiveProRight(Long houseId){
		super.checkParameterIsNull(houseId, "common:parameters_null_exception,ID");
		ProRightDO proRightDo = this.proRightDao.getEffectiveProRightByHouseId(houseId);
		if (BlankUtil.isBlank(proRightDo)) {
			return true;
		}
		this.proRightDao.deleteObject(proRightDo);

		return true;
	}

	/**
	 * 功能描述: 根据产权id,查询一个产权VO对象 <br>
	 * @param proRightId
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-28  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public ProRightVO getProRightById(Long proRightId){
		super.checkParameterIsNull(proRightId, "common:parameters_null_exception,ID");
		ProRightDO proRightDo = this.proRightDao.findById(proRightId);
		return this.assembleD2V(proRightDo);
	}

	/**
	 * @param flatDao the flatDao to set
	 */
	public void setFlatDao(IFlatDao flatDao) {
		this.flatDao = flatDao;
	}

	/**
	 * @param proRightDao the proRightDao to set
	 */
	public void setProRightDao(IProRightDao proRightDao) {
		this.proRightDao = proRightDao;
	}

	/**
	 * 功能描述: 根据指定产权实体装配一个产权VO对象 <br>
	 * @param proRightDo
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-28  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private ProRightVO assembleD2V(ProRightDO proRightDo){
		ProRightVO proRightVo = null;
		if (!BlankUtil.isBlank(proRightDo)) {
			proRightVo = new ProRightVO();
			BeanUtil.convertVoAsDo(proRightDo, proRightVo);

			HouseDomain house = proRightDo.getHouse();
			proRightVo.setHouseId(house.getKeyword());
			proRightVo.setHouseNumber(house.getHouseNumber());
			SysInfo sysInfo = proRightDo.getSysInfo();//得到创建信息和修改信息
			if (!BlankUtil.isBlank(sysInfo)) {
				proRightVo.setCreator(sysInfo.getCreator());//创建人id
				proRightVo.setSysDate(sysInfo.getSysDate());//创建日期
				proRightVo.setModifier(sysInfo.getModifier());//修改人id
				proRightVo.setModifyDate(sysInfo.getModifyDate());//修改日期
			}
		}
		return proRightVo;
	}

	/**
	 * 功能描述:  <br>
	 * @param proRightVo
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-28  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private ProRightDO assembleV2D(ProRightVO proRightVo){
		ProRightDO proRightDo = null;
		if (BlankUtil.isBlank(proRightVo.getProRightId())) {
			proRightDo = new ProRightDO();
			SysInfo sysInfo = super.createInfo(proRightVo.getCreator());
			proRightDo.setSysInfo(sysInfo);//创建者和修改者信息
			HouseDomain houseDO = this.flatDao.findById(proRightVo.getHouseId());
			proRightDo.setHouse(houseDO);
		}else{
			proRightDo = this.proRightDao.findById(proRightVo.getProRightId());
			super.modifyInfo(proRightDo.getSysInfo(), proRightVo.getModifier());
		}
		BeanUtil.convertVoAsDo(proRightVo, proRightDo);
		return proRightDo;
	}
}
